package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.MdmHelpActivity;
import cn.com.connext.scrm.repository.master.MdmHelpActivityRepository;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.web.rest.vm.MdmHelpActivityPageVM;
import cn.com.connext.scrm.web.rest.vm.MdmHelpActivityVM;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.ZonedDateTime;
import java.util.LinkedList;
import java.util.List;


@Service
@Transactional
public class MdmHelpActivityService {
        @Autowired
        private MdmHelpActivityRepository mdmHelpActivityRepository;

        public JsonResult save(MdmHelpActivityVM req) {
            JsonResult result =new JsonResult();
            if(req ==null){
                result.setMessage("用户参数不能为空!");
                result.setState(-1);
            }else{
                MdmHelpActivity entity = new MdmHelpActivity();
                BeanUtils.copyProperties(req,entity);
                entity.setCreateTime(ZonedDateTime.now());
                mdmHelpActivityRepository.saveAndFlush(entity);
                result.setMessage("添加成功");
                result.setState(0);
            }
            return result;
        }

    public JsonResult update(MdmHelpActivityVM req) {
        JsonResult result =new JsonResult();
        if(req ==null){
            result.setMessage("用户参数不能为空!");
            result.setState(-1);
        }else{
            MdmHelpActivity entity = new MdmHelpActivity();
            BeanUtils.copyProperties(req,entity);
            mdmHelpActivityRepository.saveAndFlush(entity);
            result.setMessage("添加成功");
            result.setState(0);
        }
        return result;

     }

    public Page<MdmHelpActivity> findByPage(MdmHelpActivityPageVM wocv) {
        PageRequest pageRequest = new PageRequest(wocv.getPage(), wocv.getPageSize(), Sort.Direction.DESC, "createTime");
        Page<MdmHelpActivity> page = mdmHelpActivityRepository.findAll(new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                List<Predicate> list = new LinkedList<>();
//                Predicate activityName = cb.equal(root.get("isDelete"), 2);
//                list.add(activityName);
                Predicate[] pre = new Predicate[list.size()];
                return query.where(list.toArray(pre)).getRestriction();
            }
        }, pageRequest);
        return page;
    }

    public JsonResult delete(MdmHelpActivityVM req) {
        JsonResult result =new JsonResult();
        if(req ==null || req.getId() ==null){
            result.setMessage("用户参数不能为空!");
            result.setState(-1);
        }else{
            mdmHelpActivityRepository.delete(req.getId());
            result.setMessage("删除成功");
            result.setState(0);
        }
        return result;
    }

    public JsonResult updateStatus(MdmHelpActivityVM req) {
        JsonResult result =new JsonResult();
        if(req ==null || req.getId() ==null){
            result.setMessage("用户参数不能为空!");
            result.setState(-1);
        }else{
            mdmHelpActivityRepository.updateStatus(req.getId(),req.getIsOpen());
            result.setMessage("操作成功");
            result.setState(0);
        }
        return result;
    }

    public MdmHelpActivity findOne(Long id) {
      return  mdmHelpActivityRepository.findOne(id);
    }

    public JsonResult findById(MdmHelpActivityVM req) {
        JsonResult result =new JsonResult();
        if(req ==null || req.getId() ==null){
            result.setMessage("用户参数不能为空!");
            result.setState(-1);
        }else{
            MdmHelpActivity entity = mdmHelpActivityRepository.findOne(req.getId());
            result.setMessage("数据如下");
            result.setData(entity);
            result.setState(0);
        }
        return result;
    }

}
